//
//  ShopViewController.m
//  MyProject
//
//  Created by dllo on 15/10/31.
//  Copyright (c) 2015年 蓝鸥0825. All rights reserved.
//

#import "ShopViewController.h"
#import "WHZAfnetWorking.h"
#import "tableView.h"
#import "tableViewModel.h"
#import "CollectionFirstView.h"
#import <MJRefresh.h>
#import "Save.h"


@interface ShopViewController ()

@property(nonatomic, retain)NSMutableArray *array;
@property(nonatomic, retain)NSMutableDictionary *dictionary;
@property(nonatomic, retain)tableView *ta;
@property(nonatomic, assign)NSInteger num;
@property(nonatomic, retain)CollectionFirstView *col;


@end

@implementation ShopViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商城";
    
    [Save isHaveFolder];
    [self createTable];
    [self network];
    [self createHeaderView];
    
   
    
  
#pragma mark - 下拉刷新
    self.ta.tableView.header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        [self.ta.tableView.header beginRefreshing];
        [self createData];
        
    }];
    

}

#pragma mark - 创建 tabelView 的 headerView
- (void)createHeaderView{
    
    //      tableView 的头视图
    self.col = [[CollectionFirstView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)];
    self.ta.tableView.tableHeaderView = self.col;
    self.col.VC = self;
}




#pragma mark - 创建 tableView
- (void)createTable{
    
    //      tabelView
    self.ta = [[tableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.ta];
    self.ta.VC = self;
}





#pragma mark - 检测网络情况
- (void)network {
    
    //      检测网络情况
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
    //      开始检测
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"%ld", (long)status);
        self.num = status;
        [self createData];
    }];
//    [manager stopMonitoring];
}



#pragma mark - 数据解析
- (void)createData {
    if (self.num == 1 || self.num == 2) {
        
        NSString *body = @"c_md5=d89cf0bdeba3c1a3525e9ba464e3e231&lon=4.9E-324&format=json&source=android&lat=4.9E-324&";
        NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        
        [WHZAfnetWorking POSTJRAfnNetworkingWithUrl:@"http://api.meishi.cc/v5/buy_index.php?format=json" WithParameters:dic Block:^(id result) {
            NSLog(@"%@", result);
            self.dictionary = result;
            self.array = [tableViewModel baseModelByArr:self.dictionary[@"end_list"]];
            self.ta.array = self.array;
            [self.ta.tableView.header endRefreshing];
            
            for (tableViewModel *model in self.array) {
                if ([Save isHaveModelInPlist:model]) {
                    
                }else {
                    [Save saveModelToPlist:model];
                }
            }
            [self.ta.tableView reloadData];
            
        }];
    }
    else{
        NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"caches.plist"];
        self.array = [NSMutableArray array];
        self.array = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
        NSLog(@"%@",self.array);
        self.ta.array = self.array;
        [self.ta.tableView reloadData];
        
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end













